package com.example.cart.utils;

import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.cart.R;
import com.example.cart.dto.Goods;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

public class GoodsDetailUtil {

    public static void renderGoodsUnit(Goods good, View goodsItemView) {
        TextView goodsName = goodsItemView.findViewById(R.id.goodsName);
        if(good==null){
            goodsName.setText("无效商品");
            return;
        }

        goodsName.setText(good.getName());

        ImageView goodsImage = goodsItemView.findViewById(R.id.goodsImage);
        try (InputStream in = Files.newInputStream(Paths.get(good.getImageSrc()))){
            goodsImage.setImageBitmap(
                    BitmapFactory.decodeStream(in)
            );
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        TextView goodsDetail = goodsItemView.findViewById(R.id.goodsDetail);
        goodsDetail.setText(good.getDetail());

        TextView goodsPrice = goodsItemView.findViewById(R.id.goodsPrice);
        goodsPrice.setText(good.getPrice());
    }
}
